/*
   Copyright (c) 2019 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZMindMap is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details.

*/

package xuanzi.commons.graphics;
 

public class BoxF {

	 public float left;
	    public float top;
	    public float right;
	    public float bottom;
	    
 
	    public BoxF() {}

 
	    public BoxF(float left, float top, float right, float bottom) {
	        this.left = left;
	        this.top = top;
	        this.right = right;
	        this.bottom = bottom;
	    }

 
	    public BoxF(BoxF r) {
	        if (r == null) {
	            left = top = right = bottom = 0.0f;
	        } else {
	            left = r.left;
	            top = r.top;
	            right = r.right;
	            bottom = r.bottom;
	        }
	    }
	    
 

	    @Override
	    public boolean equals(Object o) {
	        if (this == o) return true;
	        if (o == null || getClass() != o.getClass()) return false;

	        BoxF r = (BoxF) o;
	        return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
	    }

	    @Override
	    public int hashCode() {
	        int result = (left != +0.0f ? Float.floatToIntBits(left) : 0);
	        result = 31 * result + (top != +0.0f ? Float.floatToIntBits(top) : 0);
	        result = 31 * result + (right != +0.0f ? Float.floatToIntBits(right) : 0);
	        result = 31 * result + (bottom != +0.0f ? Float.floatToIntBits(bottom) : 0);
	        return result;
	    }

	    public String toString() {
	        return "RectF(" + left + ", " + top + ", "
	                      + right + ", " + bottom + ")";
	    }

	    
	    public String toShortString() {
	        return toShortString(new StringBuilder(32));
	    }
	    
	  
	    public String toShortString(StringBuilder sb) {
	        sb.setLength(0);
	        sb.append('['); sb.append(left); sb.append(',');
	        sb.append(top); sb.append("]["); sb.append(right);
	        sb.append(','); sb.append(bottom); sb.append(']');
	        return sb.toString();
	    }
	    
	 
	    public final boolean isEmpty() {
	        return left >= right || top >= bottom;
	    }

 
	    public final float width() {
	        return right - left;
	    }

 
	    public final float height() {
	        return bottom - top;
	    }

 
	    public final float centerX() {
	        return (left + right) * 0.5f;
	    }

 
	    public final float centerY() {
	        return (top + bottom) * 0.5f;
	    }
	    
	 
	    public void setEmpty() {
	        left = right = top = bottom = 0;
	    }
	    
 
	    public void set(float left, float top, float right, float bottom) {
	        this.left   = left;
	        this.top    = top;
	        this.right  = right;
	        this.bottom = bottom;
	    }

 
	    public void set(BoxF src) {
	        this.left   = src.left;
	        this.top    = src.top;
	        this.right  = src.right;
	        this.bottom = src.bottom;
	    }
	     
	    public void offset(float dx, float dy) {
	        left    += dx;
	        top     += dy;
	        right   += dx;
	        bottom  += dy;
	    }

 
	    public void offsetTo(float newLeft, float newTop) {
	        right += newLeft - left;
	        bottom += newTop - top;
	        left = newLeft;
	        top = newTop;
	    }
	    
 
	    public void inset(float dx, float dy) {
	        left    += dx;
	        top     += dy;
	        right   -= dx;
	        bottom  -= dy;
	    }

 
	    public boolean contains(float x, float y) {
	        return left < right && top < bottom   
	                && x >= left && x < right && y >= top && y < bottom;
	    }
	    
 
	    public boolean contains(float left, float top, float right, float bottom) { 
	        return this.left < this.right && this.top < this.bottom 
	                && this.left <= left && this.top <= top
	                && this.right >= right && this.bottom >= bottom;
	    }
	    
 
	    public boolean contains(BoxF r) { 
	        return this.left < this.right && this.top < this.bottom 
	                && left <= r.left && top <= r.top
	                && right >= r.right && bottom >= r.bottom;
	    }
	    
 
	    public boolean intersect(float left, float top, float right, float bottom) {
	        if (this.left < right && left < this.right
	                && this.top < bottom && top < this.bottom) {
	            if (this.left < left) {
	                this.left = left;
	            }
	            if (this.top < top) {
	                this.top = top;
	            }
	            if (this.right > right) {
	                this.right = right;
	            }
	            if (this.bottom > bottom) {
	                this.bottom = bottom;
	            }
	            return true;
	        }
	        return false;
	    }
	    
 
	    public boolean intersect(BoxF r) {
	        return intersect(r.left, r.top, r.right, r.bottom);
	    }
	    
 
	    public boolean setIntersect(BoxF a, BoxF b) {
	        if (a.left < b.right && b.left < a.right
	                && a.top < b.bottom && b.top < a.bottom) {
	            left = Math.max(a.left, b.left);
	            top = Math.max(a.top, b.top);
	            right = Math.min(a.right, b.right);
	            bottom = Math.min(a.bottom, b.bottom);
	            return true;
	        }
	        return false;
	    }
	    
 
	    public boolean intersects(float left, float top, float right,
	                              float bottom) {
	        return this.left < right && left < this.right
	                && this.top < bottom && top < this.bottom;
	    }
	    
 
	    public static boolean intersects(BoxF a, BoxF b) {
	        return a.left < b.right && b.left < a.right
	                && a.top < b.bottom && b.top < a.bottom;
	    }
	    
	  
 
	    public void union(float left, float top, float right, float bottom) {
	        if ((left < right) && (top < bottom)) {
	            if ((this.left < this.right) && (this.top < this.bottom)) {
	                if (this.left > left)
	                    this.left = left;
	                if (this.top > top)
	                    this.top = top;
	                if (this.right < right)
	                    this.right = right;
	                if (this.bottom < bottom)
	                    this.bottom = bottom;
	            } else {
	                this.left = left;
	                this.top = top;
	                this.right = right;
	                this.bottom = bottom;
	            }
	        }
	    }
	    
 
	    public void union(BoxF r) {
	        union(r.left, r.top, r.right, r.bottom);
	    }
	    
 
	    public void union(float x, float y) {
	        if (x < left) {
	            left = x;
	        } else if (x > right) {
	            right = x;
	        }
	        if (y < top) {
	            top = y;
	        } else if (y > bottom) {
	            bottom = y;
	        }
	    }
	    
 
	    public void sort() {
	        if (left > right) {
	            float temp = left;
	            left = right;
	            right = temp;
	        }
	        if (top > bottom) {
	            float temp = top;
	            top = bottom;
	            bottom = temp;
	        }
	    }

	 
	   
}
